package com.yuanfeng.biz.front.user.web;

import com.yuanfeng.biz.front.user.service.UserService;
import com.yuanfeng.entity.Users;
import com.yuanfeng.util.BaseServlet;
import com.yuanfeng.util.MD5Util;
import com.yuanfeng.biz.front.user.service.UserServiceImpl;
import com.yuanfeng.util.WebUtil;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户模块的servlet
 * 
 * @author yuanfeng
 *
 */
public class UserServlet extends BaseServlet {
	UserService service = new UserServiceImpl();

	/**
	 * 用户请求 ： /user?action=login
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	// 登录
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取用户输入的验证码
		String code = request.getParameter("sCode");// 先验证验证码
		// 获取生成的验证码
		String sCode = (String) request.getSession().getAttribute("sCode");
		if (code.trim().equals(sCode)) {
			// 去验证用户名是否正确
			Users user = WebUtil.copyRequestToBean(request, Users.class);
			// 验证用户名
			Users loginUser = service.login(user.getName());

			if (loginUser != null) {
				// 验证密码
				if (MD5Util.md5(user.getPassword()).equals(loginUser.getPassword())) {
					// 登录成功
					// 将用户信息放到session中
					request.getSession().setAttribute("user", loginUser);
					// 转到IndexServlet页面执行对应的方法
					response.sendRedirect(request.getContextPath() + "/index?action=showIndex");
					return;
				} else {
					// 密码错误
					request.setAttribute("msg", "密码输入有误");
					request.getRequestDispatcher("/jsp/front/shopIndex.jsp").forward(request, response);
					return;
				}
			} else {
				// 用户名不存在
				request.setAttribute("msg", "用户名不存在");
				request.getRequestDispatcher("/jsp/front/shopIndex.jsp").forward(request, response);
				return;
			}
		} else {
			// 验证码错误
			request.setAttribute("msg", "验证码错误");
			request.getRequestDispatcher("/jsp/front/shopIndex.jsp").forward(request, response);
			return;
		}
	}

	// 用户的登出
	public void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 域对象中得到用户的信息删除
		request.getSession().removeAttribute("user");
		response.sendRedirect(request.getContextPath() + "/index?action=showIndex");
	}

	// 用户的注册
	public void register(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 拷贝数据
		Users user = WebUtil.copyRequestToBean(request, Users.class);
		System.out.println(user.getName());
		// 保存
		user = service.reg(user);
		request.getSession().setAttribute("success", "success");
		// 跳转到注册成功页面
		response.sendRedirect(request.getContextPath() + "/jsp/front/user/regsucc.jsp");

	}

	public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Users userInfo = (Users) request.getSession().getAttribute("user");
		request.getSession().setAttribute("user", userInfo);
		// 转发到userinfo.jsp
		response.sendRedirect(request.getContextPath() + "/jsp/front/user/userinfo.jsp");

	}
}
